<template>
  <div class="search-result">
    <!-- 导航栏 -->
    <van-nav-bar
      :title="keyword + '的搜索结果'"
      left-arrow
      fixed
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->

    <!-- 文章列表 -->
    <van-list
      class="article-list"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        @click="$router.push('/article/' + item.art_id.toString())"
        v-for="item in list"
        :key="item.art_id.toString()"
        :title="item.title"
      />
    </van-list>
    <!-- /文章列表 -->
  </div>
</template>

<script>
import { getSearchList } from '@/api/search'
export default {
  name: 'SearchResult',
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      keyword: '',
      page: 1,
      per_page: 10
    }
  },
  created() {
    this.keyword = this.$route.query.keyword
  },

  methods: {
    async onLoad() {
      const res = await getSearchList({
        q: this.keyword,
        page: this.page,
        per_page: this.per_page
      })
      const { results } = res.data.data
      this.list = [...this.list, ...results]
      this.page++
      this.loading = false

      if (results.length === 0) {
        this.finished = true
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.search-result {
  .article-list {
    margin-top: 39px;
  }
}
</style>
